﻿using Autofac;
using Autofac.Extensions.DependencyInjection;
using Commonality.RedisHelper.Service;
using Microsoft.AspNetCore.Mvc;
using Microsoft.Extensions.Options;
using SqlSugar;
using System.Reflection;

namespace LivePlatAPI.Utillity.AutoFac
{
    public static class HostBuildService
    {
        public static void HostBuildServiceAutoFac(this WebApplicationBuilder builder)
        {

            var controllerBaseTypes = typeof(ControllerBase);
            //添加autofac替换IOC
            builder.Host.UseServiceProviderFactory(new AutofacServiceProviderFactory());

            string? connectionString = builder.Configuration.GetConnectionString("SqlSugarClient");
            //Scoped用SqlSugarClient 
            SqlSugarClient sqlSugar = new SqlSugarClient(new ConnectionConfig()
            {
                DbType = SqlSugar.DbType.Sqlite,
                ConnectionString = connectionString,
                IsAutoCloseConnection = true,
            },
           db =>
           {
               //SQL执行完
               db.Aop.OnLogExecuted = (sql, pars) =>
               {
                   //执行完了可以输出SQL执行时间 (OnLogExecutedDelegate) 
                   Console.Write("time:" + db.Ado.SqlExecutionTime.ToString());
               };
               db.Aop.OnLogExecuting = (sql, pars) => //SQL执行前
               {
                   //获取原生SQL推荐 5.1.4.63  性能OK
                   UtilMethods.GetNativeSql(sql, pars);
                   Console.WriteLine("-------------{0}", sql);
                   //获取无参数化SQL 影响性能只适合调试
                   //UtilMethods.GetSqlString(DbType.SqlServer,sql,pars)
               };
               db.Aop.OnError = (exp) =>//SQL报错
               {
                   //获取原生SQL推荐 5.1.4.63  性能OK
                   //UtilMethods.GetNativeSql(exp.sql,exp.parameters)

                   //获取无参数SQL对性能有影响，特别大的SQL参数多的，调试使用
                   UtilMethods.GetSqlString(DbType.SqlServer, exp.Sql, (SugarParameter[])exp.Parametres);
               };
               db.Aop.OnExecutingChangeSql = (sql, pars) => //可以修改SQL和参数的值
               {
                   //sql=newsql
                   //foreach(var p in pars) //修改
                   return new KeyValuePair<string, SugarParameter[]>(sql, pars);
               };
           });

            builder.Host.ConfigureContainer<ContainerBuilder>(serve =>
            {
                Assembly IService = Assembly.Load("IService");
                Assembly Service = Assembly.Load("Service");
                serve.RegisterAssemblyTypes(IService, Service).AsImplementedInterfaces();
                //注册controllerbase

                serve.RegisterAssemblyTypes(typeof(Program).Assembly).Where(t => controllerBaseTypes.IsAssignableFrom(t) && t != controllerBaseTypes);
                //注册 sqlsuger
                serve.Register<ISqlSugarClient>(sqll => sqlSugar);
                //redis
                serve.RegisterType<RedisHashService>();
                serve.RegisterType<RedisListService>();
                serve.RegisterType<RedisSetService>();
                serve.RegisterType<RedisStringService>();
                serve.RegisterType<RedisZSetService>();
            });
        }

    }

}
